// 图片转成base64格式
export const getBase64 = (file: File): Promise<string> => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result as string);
    reader.onerror = (error) => reject(error);
  });
};

// 方法2：解析成对象
export const parseQuery = (search: string) => {
  if (!search) return {};
  const params: any = {};
  const searchStr = search.startsWith('?') ? search.substring(1) : search;
  const pairs = searchStr.split('&');

  for (let pair of pairs) {
    const [key, value] = pair.split('=');
    if (key) {
      params[decodeURIComponent(key)] = decodeURIComponent(value || '');
    }
  }

  return params;
};

export const getType = (data: any) => {
  const typeName = Object.prototype.toString.call(data).toLowerCase();
  return typeName.substring(8, typeName.length - 1);
};
